<?php
namespace Common\Model;
use Think\Model;

class MessageModel extends Model{
	protected $_auto = array(/* array(完成字段1,完成规则,[完成条件,附加规则]) */
			array('addTime', 'time', 1, 'function'),
			array('updateTime', 'time', 2, 'function'),
			array('isOn', '1', 1),
	);  // 自动完成定义
	
	
	/*
	 * 获取消息列表
	*/
	public function getMessageList($pageNo = 1, $pageSize = 15, $where = array(), $order = array(), $field = "*") {
		$rt = $this->field($field)->where($where)->page("$pageNo,$pageSize")->order($order)->select();
		return $rt;
	}
	

	/*
	 * 获取消息列表总数
	*/
	public function getMessageListLength($where = array()) {
		$result = $this->where($where)->count();
		return $result;
	}
	
	/*
	 * 添加
	*/
	public function addMessage($data = '') {
		$rt = false;
		if ($this->create($data)) {
			$rt = $this->add();
		}
		return $rt;
	}
	
	/*
	 * 更新
	*/
	public function updateMessage($data = '') {
		$rt = false;
		if ($this->create($data)) {
			$rt = $this->save();
		}
		return $rt;
	}
	
	/*
	 * 获取一条
	*/
	public function getMessage($messageID,$where=array(),$field = "*") {
		if(!empty($messageID)) $where['messageID']=$messageID;
		$message = $this->field($field)->where($where)->find();  //，find()返回一个一维数组，select()返回一个二维数组
		return $message;          //返回一维数组
	}
	
	/*
	 * 移除，只是隐藏，并非真的从数据库中删除数据
	*/
	public function deleteMessage($messageID) {
		$data['isOn'] = 0;
		$data['messageID'] = $messageID;
		$message = $this->save($data);
		return $message;          //成功返回一个真值
	}
	
}
?>